import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

/**
 * Represents the store screen.
 */
public class StorePanel extends JPanel  implements Serializable{

	private JTextField textFieldOxen;
	private JTextField textFieldFood;
	private JTextField textFieldClothing;
	private JTextField textFieldAmmo;
	private JTextField textFieldOrder;
	private JTextField textFieldAxles;
	private JTextField textFieldTongues;
	private JLabel lblOxenQty;
	private JLabel lblFoodQty;
	private JLabel lblClothingQty;
	private JLabel lblAmmoQty;
	private JLabel lblWheelsQty;
	private JLabel lblAxlesQty;
	private JLabel lblTonguesQty;
	private JTextField[] textFields;
	private JButton btnPurchaseButton;
	private int[] quants;
	
	
	
	private Game game;
	private Store s;
	
	/*
	 * TODO change implementation to take no Store item in (UI elements shouldn't have access anyways),
	 * only need one store(maybe) and one store panel for entire game. Every time storepanel is about to be shown,
	 * update labels from Game class (switchScreen method) using StorePanel.setLabel(String s) methods or something.
	 * 
	 * TODO implement Cancel button to be disabled if the store is not the first store (or maybe the user can
	 * be allowed to leave the first store without buying anything, if he/she so chooses). If that's the case,
	 * another implementation might be to just have the "Purchase" button display "Cancel" when no items have
	 * been entered, and switch to "Purchase" when at least 1 item has been entered. 
	 * 
	 * TODO bounds checking for when users enter negative numbers, numbers too long to store in int (+/- 2billion-something), letters/symbols, etc.
	 *  	(some of this is done already: negative numbers are checked in game's attemptPurchases method)
	 *  
	 *  numberField = new JFormattedTextField(NumberFormat.getInstance()); <-- **Allows only numbers to be inserted**
	 *  
	 *  TODO Show money and weight remaining on screen. Possibly show projected weight and cost of items user has entered but not clicked "Purchase" yet.
	 */
	
	/**  
	 * Creates the store screen.
	 * 
	 * @param s - the universal store object.
	 */
	public StorePanel(Store s, Game g) {
		
		//FIXME Shouldn't have access to store object.
		
		game = g;
		this.s = s;
		setPreferredSize(new Dimension(800,600));
		
		quants = new int[7];
		
		DecimalFormat df = new DecimalFormat("$#,###,##0.00");
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(null);
		
		JLabel lblStore = new JLabel("Store");
		lblStore.setHorizontalAlignment(SwingConstants.CENTER);
		lblStore.setFont(new Font("Arial", Font.PLAIN, 24));
		lblStore.setBounds(265, 11, 231, 55);
		add(lblStore);
		
		JLabel lblOxen = new JLabel("Oxen");
		lblOxen.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblOxen.setBounds(27, 127, 46, 14);
		add(lblOxen);
		
		JLabel lblFood = new JLabel("Food");
		lblFood.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblFood.setBounds(27, 167, 91, 14);
		add(lblFood);
		
		JLabel lblClothing = new JLabel("Clothing");
		lblClothing.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblClothing.setBounds(27, 211, 91, 14);
		add(lblClothing);
		
		JLabel lblAmmunition = new JLabel("Ammunition");
		lblAmmunition.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblAmmunition.setBounds(27, 252, 75, 14);
		add(lblAmmunition);
		
		JLabel lblTongues = new JLabel("Wagon Tongues");
		lblTongues.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblTongues.setBounds(396, 210, 107, 14);
		add(lblTongues);
		
		JLabel lblWheels = new JLabel("Wagon Wheels");
		lblWheels.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblWheels.setBounds(396, 126, 97, 14);
		add(lblWheels);
		
		JLabel lblAxles = new JLabel("Wagon Axles");
		lblAxles.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblAxles.setBounds(396, 166, 97, 14);
		add(lblAxles);
		
		lblOxenQty = new JLabel("(x" + (s.getQuantities())[0] + ")");
		lblOxenQty.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblOxenQty.setBounds(113, 127, 79, 14);
		add(lblOxenQty);
		
		lblFoodQty = new JLabel("(x" + (s.getQuantities())[1] + ")");
		lblFoodQty.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblFoodQty.setBounds(113, 167, 79, 14);
		add(lblFoodQty);
		
		lblClothingQty = new JLabel("(x" + (s.getQuantities())[2] + ")");
		lblClothingQty.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblClothingQty.setBounds(113, 211, 79, 14);
		add(lblClothingQty);
		
		lblAmmoQty = new JLabel("(x" + (s.getQuantities())[3] + ")");
		lblAmmoQty.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblAmmoQty.setBounds(113, 252, 79, 14);
		add(lblAmmoQty);
		
		lblWheelsQty = new JLabel("(x" + (s.getQuantities())[4] + ")");
		lblWheelsQty.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblWheelsQty.setBounds(505, 127, 80, 14);
		add(lblWheelsQty);
		
		lblAxlesQty = new JLabel("(x" + (s.getQuantities())[5] + ")");
		lblAxlesQty.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblAxlesQty.setBounds(505, 167, 80, 14);
		add(lblAxlesQty);
		
		lblTonguesQty = new JLabel("(x" + (s.getQuantities())[6] + ")");
		lblTonguesQty.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblTonguesQty.setBounds(505, 211, 80, 14);
		add(lblTonguesQty);
		
		textFields = new JTextField[7];
		
		textFieldOxen = new JTextField();
		textFieldOxen.setBounds(199, 124, 86, 20);
		textFieldOxen.setText("0");
		add(textFieldOxen);
		textFieldOxen.setColumns(10);
		textFields[0] = textFieldOxen;
		
		textFieldFood = new JTextField();
		textFieldFood.setBounds(199, 164, 86, 20);
		textFieldFood.setText("0");
		add(textFieldFood);
		textFieldFood.setColumns(10);
		textFields[1] = textFieldFood;
		
		textFieldClothing = new JTextField();
		textFieldClothing.setBounds(199, 208, 86, 20);
		add(textFieldClothing);
		textFieldClothing.setColumns(10);
		textFieldClothing.setText("0");
		textFields[2] = textFieldClothing;
		
		textFieldAmmo = new JTextField();
		textFieldAmmo.setBounds(199, 249, 86, 20);
		add(textFieldAmmo);
		textFieldAmmo.setColumns(10);
		textFieldAmmo.setText("0");
		textFields[3] = textFieldAmmo;
		
		textFieldOrder = new JTextField();
		textFieldOrder.setBounds(594, 124, 86, 20);
		add(textFieldOrder);
		textFieldOrder.setColumns(10);
		textFieldOrder.setText("0");
		textFields[4] = textFieldOrder;
		
		textFieldAxles = new JTextField();
		textFieldAxles.setBounds(594, 164, 86, 20);
		add(textFieldAxles);
		textFieldAxles.setColumns(10);
		textFieldAxles.setText("0");
		textFields[5] = textFieldAxles;
		
		textFieldTongues = new JTextField();
		textFieldTongues.setBounds(594, 208, 86, 20);
		add(textFieldTongues);
		textFieldTongues.setColumns(10);
		textFieldTongues.setText("0");
		textFields[6] = textFieldTongues;
		
		btnPurchaseButton = new JButton("Purchase");
		btnPurchaseButton.setBounds(304, 413, 178, 55);
		add(btnPurchaseButton);
		btnPurchaseButton.addActionListener(new PurchaseButtonListener());
		
		JLabel lblItem = new JLabel("Item");
		lblItem.setFont(new Font("Arial", Font.BOLD, 11));
		lblItem.setBounds(27, 88, 46, 14);
		add(lblItem);
		
		JLabel lblQty = new JLabel("Quantity");
		lblQty.setFont(new Font("Arial", Font.BOLD, 11));
		lblQty.setBounds(106, 89, 86, 14);
		add(lblQty);
		
		JLabel lblOrder = new JLabel("Order");
		lblOrder.setFont(new Font("Arial", Font.BOLD, 11));
		lblOrder.setBounds(205, 88, 46, 14);
		add(lblOrder);
		
		JLabel lblItem2 = new JLabel("Item");
		lblItem2.setFont(new Font("Arial", Font.BOLD, 11));
		lblItem2.setBounds(396, 88, 46, 14);
		add(lblItem2);
		
		JLabel lblQty2 = new JLabel("Quantity");
		lblQty2.setFont(new Font("Arial", Font.BOLD, 11));
		lblQty2.setBounds(501, 88, 86, 14);
		add(lblQty2);
		
		JLabel lblOrder2 = new JLabel("Order");
		lblOrder2.setFont(new Font("Arial", Font.BOLD, 11));
		lblOrder2.setBounds(617, 88, 46, 14);
		add(lblOrder2);
		
		JLabel lblPrice = new JLabel("Price");
		lblPrice.setFont(new Font("Arial", Font.BOLD, 11));
		lblPrice.setBounds(319, 88, 46, 14);
		add(lblPrice);
		
		JLabel lblPriceOxen = new JLabel(df.format((s.getCosts())[0] / 100.0));
		lblPriceOxen.setBounds(319, 127, 46, 14);
		add(lblPriceOxen);
		
		JLabel lblPriceFood = new JLabel(df.format((s.getCosts())[1] / 100.0));
		lblPriceFood.setBounds(319, 167, 46, 14);
		add(lblPriceFood);
		
		JLabel lblPriceClothing = new JLabel(df.format((s.getCosts())[2] / 100.0));
		lblPriceClothing.setBounds(319, 211, 46, 14);
		add(lblPriceClothing);
		
		JLabel lblPriceAmmo = new JLabel(df.format((s.getCosts())[3] / 100.0));
		lblPriceAmmo.setBounds(319, 252, 46, 14);
		add(lblPriceAmmo);
		
		JLabel lblPriceWheels = new JLabel(df.format((s.getCosts())[4] / 100.0));
		lblPriceWheels.setBounds(712, 127, 46, 14);
		add(lblPriceWheels);
		
		JLabel lblPriceAxles = new JLabel(df.format((s.getCosts())[5] / 100.0));
		lblPriceAxles.setBounds(712, 167, 46, 14);
		add(lblPriceAxles);
		
		JLabel lblPriceTongues = new JLabel(df.format((s.getCosts())[6] / 100.0));
		lblPriceTongues.setBounds(712, 211, 46, 14);
		add(lblPriceTongues);
		
		JLabel lblPrice2 = new JLabel("Price");
		lblPrice2.setFont(new Font("Arial", Font.BOLD, 11));
		lblPrice2.setBounds(712, 88, 46, 14);
		add(lblPrice2);
	}
	
	
	/**
	 * Accessor for the quants field.
	 * @return List of desired quantities of items.
	 */
	public int[] getQuants()
	{
		return quants;
	}
	
	private void updateText()
	{
		for(int i = 0; i < textFields.length; i++)
			textFields[i].setText("0");
		
		lblOxenQty.setText("(x" + (s.getQuantities())[0] + ")");
		lblFoodQty.setText("(x" + (s.getQuantities())[1] + ")");
		lblClothingQty.setText("(x" + (s.getQuantities())[2] + ")");
		lblAmmoQty.setText("(x" + (s.getQuantities())[3] + ")");
		lblWheelsQty.setText("(x" + (s.getQuantities())[4] + ")");
		lblAxlesQty.setText("(x" + (s.getQuantities())[5] + ")");
		lblTonguesQty.setText("(x" + (s.getQuantities())[6] + ")");
	}
	
	/**
	 * ActionListener for the "Purchase" button on the store screen.
	 */
	private class PurchaseButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			if(!(textFieldOxen.getText().equals("") && textFieldFood.getText().equals("") && textFieldClothing.getText().equals("") && textFieldAmmo.getText().equals("") && textFieldOrder.getText().equals("") && textFieldAxles.getText().equals("") && textFieldTongues.getText().equals("")))
			{
				for(int i = 0; i < textFields.length; i++)
				{
					try {
						quants[i] = Integer.parseInt(textFields[i].getText());

					} 
					catch(NumberFormatException e) { 
						//FIXME Notice to user that field wasn't an int, break loop & allow user to try again
						quants[i] = 0;
					}
				}
				boolean successfulPurchase = (game.attemptPurchase(quants));
				if (successfulPurchase){
					game.changePanel("TurnPanel");
					updateText();
				}
			}
			else {
				//FIXME Dialog box  instead. Allow user to "purchase" nothing? Add a cancel button
				System.out.println("You can't use the restroom without buying anything.");
			}
		}
	}
}
